<!-- HTML header for doxygen 1.8.6-->
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/xhtml;charset=UTF-8"/>
<meta http-equiv="X-UA-Compatible" content="IE=9"/>
<meta name="generator" content="Doxygen 1.8.13"/>
<title>OpenCV: samples/cpp/stitching.cpp</title>
<link href="../../opencv.ico" rel="shortcut icon" type="image/x-icon" />
<link href="../../tabs.css" rel="stylesheet" type="text/css"/>
<script type="text/javascript" src="../../jquery.js"></script>
<script type="text/javascript" src="../../dynsections.js"></script>
<script type="text/javascript" src="../../tutorial-utils.js"></script>
<link href="../../search/search.css" rel="stylesheet" type="text/css"/>
<script type="text/javascript" src="../../search/searchdata.js"></script>
<script type="text/javascript" src="../../search/search.js"></script>
<script type="text/x-mathjax-config">
  MathJax.Hub.Config({
    extensions: ["tex2jax.js", "TeX/AMSmath.js", "TeX/AMSsymbols.js"],
    jax: ["input/TeX","output/HTML-CSS"],
});
//<![CDATA[
MathJax.Hub.Config(
{
  TeX: {
      Macros: {
          matTT: [ "\\[ \\left|\\begin{array}{ccc} #1 & #2 & #3\\\\ #4 & #5 & #6\\\\ #7 & #8 & #9 \\end{array}\\right| \\]", 9],
          fork: ["\\left\\{ \\begin{array}{l l} #1 & \\mbox{#2}\\\\ #3 & \\mbox{#4}\\\\ \\end{array} \\right.", 4],
          forkthree: ["\\left\\{ \\begin{array}{l l} #1 & \\mbox{#2}\\\\ #3 & \\mbox{#4}\\\\ #5 & \\mbox{#6}\\\\ \\end{array} \\right.", 6],
          forkfour: ["\\left\\{ \\begin{array}{l l} #1 & \\mbox{#2}\\\\ #3 & \\mbox{#4}\\\\ #5 & \\mbox{#6}\\\\ #7 & \\mbox{#8}\\\\ \\end{array} \\right.", 8],
          vecthree: ["\\begin{bmatrix} #1\\\\ #2\\\\ #3 \\end{bmatrix}", 3],
          vecthreethree: ["\\begin{bmatrix} #1 & #2 & #3\\\\ #4 & #5 & #6\\\\ #7 & #8 & #9 \\end{bmatrix}", 9],
          cameramatrix: ["#1 = \\begin{bmatrix} f_x & 0 & c_x\\\\ 0 & f_y & c_y\\\\ 0 & 0 & 1 \\end{bmatrix}", 1],
          distcoeffs: ["(k_1, k_2, p_1, p_2[, k_3[, k_4, k_5, k_6 [, s_1, s_2, s_3, s_4[, \\tau_x, \\tau_y]]]]) \\text{ of 4, 5, 8, 12 or 14 elements}"],
          distcoeffsfisheye: ["(k_1, k_2, k_3, k_4)"],
          hdotsfor: ["\\dots", 1],
          mathbbm: ["\\mathbb{#1}", 1],
          bordermatrix: ["\\matrix{#1}", 1]
      }
  }
}
);
//]]>
</script><script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.0/MathJax.js"></script>
<link href="../../doxygen.css" rel="stylesheet" type="text/css" />
<link href="../../stylesheet.css" rel="stylesheet" type="text/css"/>
</head>
<body>
<div id="top"><!-- do not remove this div, it is closed by doxygen! -->
<div id="titlearea">
<!--#include virtual="/google-search.html"-->
<table cellspacing="0" cellpadding="0">
 <tbody>
 <tr style="height: 56px;">
  <td id="projectlogo"><img alt="Logo" src="../../opencv-logo-small.png"/></td>
  <td style="padding-left: 0.5em;">
   <div id="projectname">OpenCV
   &#160;<span id="projectnumber">4.5.2</span>
   </div>
   <div id="projectbrief">Open Source Computer Vision</div>
  </td>
 </tr>
 </tbody>
</table>
</div>
<!-- end header part -->
<!-- Generated by Doxygen 1.8.13 -->
<script type="text/javascript">
var searchBox = new SearchBox("searchBox", "../../search",false,'Search');
</script>
<script type="text/javascript" src="../../menudata.js"></script>
<script type="text/javascript" src="../../menu.js"></script>
<script type="text/javascript">
$(function() {
  initMenu('../../',true,false,'search.php','Search');
  $(document).ready(function() { init_search(); });
});
</script>
<div id="main-nav"></div>
</div><!-- top -->
<!-- window showing the filter options -->
<div id="MSearchSelectWindow"
     onmouseover="return searchBox.OnSearchSelectShow()"
     onmouseout="return searchBox.OnSearchSelectHide()"
     onkeydown="return searchBox.OnSearchSelectKey(event)">
</div>

<!-- iframe showing the search results (closed by default) -->
<div id="MSearchResultsWindow">
<iframe src="javascript:void(0)" frameborder="0" 
        name="MSearchResults" id="MSearchResults">
</iframe>
</div>

<div class="header">
  <div class="headertitle">
<div class="title">samples/cpp/stitching.cpp</div>  </div>
</div><!--header-->
<div class="contents">
<p>A basic example on image stitching</p>
<div class="fragment"><div class="line"></div><div class="line"><span class="preprocessor">#include &quot;<a class="code" href="../../d6/d87/imgcodecs_8hpp.html">opencv2/imgcodecs.hpp</a>&quot;</span></div><div class="line"><span class="preprocessor">#include &quot;<a class="code" href="../../d4/dd5/highgui_8hpp.html">opencv2/highgui.hpp</a>&quot;</span></div><div class="line"><span class="preprocessor">#include &quot;<a class="code" href="../../dc/d7a/stitching_8hpp.html">opencv2/stitching.hpp</a>&quot;</span></div><div class="line"></div><div class="line"><span class="preprocessor">#include &lt;iostream&gt;</span></div><div class="line"></div><div class="line"><span class="keyword">using namespace </span><a class="code" href="../../d8/dcc/namespacestd.html">std</a>;</div><div class="line"><span class="keyword">using namespace </span><a class="code" href="../../d2/d75/namespacecv.html">cv</a>;</div><div class="line"></div><div class="line"><span class="keywordtype">bool</span> divide_images = <span class="keyword">false</span>;</div><div class="line"><a class="code" href="../../d2/d8d/classcv_1_1Stitcher.html#a114713924ec05a0309f4df7e918c0324">Stitcher::Mode</a> mode = Stitcher::PANORAMA;</div><div class="line">vector&lt;Mat&gt; imgs;</div><div class="line"><span class="keywordtype">string</span> result_name = <span class="stringliteral">&quot;result.jpg&quot;</span>;</div><div class="line"></div><div class="line"><span class="keywordtype">void</span> printUsage(<span class="keywordtype">char</span>** argv);</div><div class="line"><span class="keywordtype">int</span> parseCmdArgs(<span class="keywordtype">int</span> argc, <span class="keywordtype">char</span>** argv);</div><div class="line"></div><div class="line"><span class="keywordtype">int</span> main(<span class="keywordtype">int</span> argc, <span class="keywordtype">char</span>* argv[])</div><div class="line">{</div><div class="line">    <span class="keywordtype">int</span> retval = parseCmdArgs(argc, argv);</div><div class="line">    <span class="keywordflow">if</span> (retval) <span class="keywordflow">return</span> EXIT_FAILURE;</div><div class="line"></div><div class="line">    <a name="_a0"></a><a class="code" href="../../d3/d63/classcv_1_1Mat.html">Mat</a> pano;</div><div class="line">    <a class="code" href="../../dc/d84/group__core__basic.html#ga6395ca871a678020c4a31fadf7e8cc63">Ptr&lt;Stitcher&gt;</a> stitcher = Stitcher::create(mode);</div><div class="line">    <a class="code" href="../../d2/d8d/classcv_1_1Stitcher.html#a507409ce9435dd89857469d12ec06b45">Stitcher::Status</a> status = stitcher-&gt;stitch(imgs, pano);</div><div class="line"></div><div class="line">    <span class="keywordflow">if</span> (status != Stitcher::OK)</div><div class="line">    {</div><div class="line">        cout &lt;&lt; <span class="stringliteral">&quot;Can&#39;t stitch images, error code = &quot;</span> &lt;&lt; int(status) &lt;&lt; endl;</div><div class="line">        <span class="keywordflow">return</span> EXIT_FAILURE;</div><div class="line">    }</div><div class="line"></div><div class="line">    <a name="a1"></a><a class="code" href="../../d4/da8/group__imgcodecs.html#gabbc7ef1aa2edfaa87772f1202d67e0ce">imwrite</a>(result_name, pano);</div><div class="line">    cout &lt;&lt; <span class="stringliteral">&quot;stitching completed successfully\n&quot;</span> &lt;&lt; result_name &lt;&lt; <span class="stringliteral">&quot; saved!&quot;</span>;</div><div class="line">    <span class="keywordflow">return</span> EXIT_SUCCESS;</div><div class="line">}</div><div class="line"></div><div class="line"></div><div class="line"><span class="keywordtype">void</span> printUsage(<span class="keywordtype">char</span>** argv)</div><div class="line">{</div><div class="line">    cout &lt;&lt;</div><div class="line">         <span class="stringliteral">&quot;Images stitcher.\n\n&quot;</span> &lt;&lt; <span class="stringliteral">&quot;Usage :\n&quot;</span> &lt;&lt; argv[0] &lt;&lt;<span class="stringliteral">&quot; [Flags] img1 img2 [...imgN]\n\n&quot;</span></div><div class="line">         <span class="stringliteral">&quot;Flags:\n&quot;</span></div><div class="line">         <span class="stringliteral">&quot;  --d3\n&quot;</span></div><div class="line">         <span class="stringliteral">&quot;      internally creates three chunks of each image to increase stitching success\n&quot;</span></div><div class="line">         <span class="stringliteral">&quot;  --mode (panorama|scans)\n&quot;</span></div><div class="line">         <span class="stringliteral">&quot;      Determines configuration of stitcher. The default is &#39;panorama&#39;,\n&quot;</span></div><div class="line">         <span class="stringliteral">&quot;      mode suitable for creating photo panoramas. Option &#39;scans&#39; is suitable\n&quot;</span></div><div class="line">         <span class="stringliteral">&quot;      for stitching materials under affine transformation, such as scans.\n&quot;</span></div><div class="line">         <span class="stringliteral">&quot;  --output &lt;result_img&gt;\n&quot;</span></div><div class="line">         <span class="stringliteral">&quot;      The default is &#39;result.jpg&#39;.\n\n&quot;</span></div><div class="line">         <span class="stringliteral">&quot;Example usage :\n&quot;</span> &lt;&lt; argv[0] &lt;&lt; <span class="stringliteral">&quot; --d3 --mode scans img1.jpg img2.jpg\n&quot;</span>;</div><div class="line">}</div><div class="line"></div><div class="line"></div><div class="line"><span class="keywordtype">int</span> parseCmdArgs(<span class="keywordtype">int</span> argc, <span class="keywordtype">char</span>** argv)</div><div class="line">{</div><div class="line">    <span class="keywordflow">if</span> (argc == 1)</div><div class="line">    {</div><div class="line">        printUsage(argv);</div><div class="line">        <span class="keywordflow">return</span> EXIT_FAILURE;</div><div class="line">    }</div><div class="line"></div><div class="line">    <span class="keywordflow">for</span> (<span class="keywordtype">int</span> i = 1; i &lt; argc; ++i)</div><div class="line">    {</div><div class="line">        <span class="keywordflow">if</span> (<span class="keywordtype">string</span>(argv[i]) == <span class="stringliteral">&quot;--help&quot;</span> || <span class="keywordtype">string</span>(argv[i]) == <span class="stringliteral">&quot;/?&quot;</span>)</div><div class="line">        {</div><div class="line">            printUsage(argv);</div><div class="line">            <span class="keywordflow">return</span> EXIT_FAILURE;</div><div class="line">        }</div><div class="line">        <span class="keywordflow">else</span> <span class="keywordflow">if</span> (<span class="keywordtype">string</span>(argv[i]) == <span class="stringliteral">&quot;--d3&quot;</span>)</div><div class="line">        {</div><div class="line">            divide_images = <span class="keyword">true</span>;</div><div class="line">        }</div><div class="line">        <span class="keywordflow">else</span> <span class="keywordflow">if</span> (<span class="keywordtype">string</span>(argv[i]) == <span class="stringliteral">&quot;--output&quot;</span>)</div><div class="line">        {</div><div class="line">            result_name = argv[i + 1];</div><div class="line">            i++;</div><div class="line">        }</div><div class="line">        <span class="keywordflow">else</span> <span class="keywordflow">if</span> (<span class="keywordtype">string</span>(argv[i]) == <span class="stringliteral">&quot;--mode&quot;</span>)</div><div class="line">        {</div><div class="line">            <span class="keywordflow">if</span> (<span class="keywordtype">string</span>(argv[i + 1]) == <span class="stringliteral">&quot;panorama&quot;</span>)</div><div class="line">                mode = Stitcher::PANORAMA;</div><div class="line">            <span class="keywordflow">else</span> <span class="keywordflow">if</span> (<span class="keywordtype">string</span>(argv[i + 1]) == <span class="stringliteral">&quot;scans&quot;</span>)</div><div class="line">                mode = Stitcher::SCANS;</div><div class="line">            <span class="keywordflow">else</span></div><div class="line">            {</div><div class="line">                cout &lt;&lt; <span class="stringliteral">&quot;Bad --mode flag value\n&quot;</span>;</div><div class="line">                <span class="keywordflow">return</span> EXIT_FAILURE;</div><div class="line">            }</div><div class="line">            i++;</div><div class="line">        }</div><div class="line">        <span class="keywordflow">else</span></div><div class="line">        {</div><div class="line">            <a class="code" href="../../d3/d63/classcv_1_1Mat.html">Mat</a> img = <a name="a2"></a><a class="code" href="../../d4/da8/group__imgcodecs.html#ga288b8b3da0892bd651fce07b3bbd3a56">imread</a>(<a name="a3"></a><a class="code" href="../../d6/dba/group__core__utils__samples.html#ga3a33b00033b46c698ff6340d95569c13">samples::findFile</a>(argv[i]));</div><div class="line">            <span class="keywordflow">if</span> (img.<a name="a4"></a><a class="code" href="../../d3/d63/classcv_1_1Mat.html#abbec3525a852e77998aba034813fded4">empty</a>())</div><div class="line">            {</div><div class="line">                cout &lt;&lt; <span class="stringliteral">&quot;Can&#39;t read image &#39;&quot;</span> &lt;&lt; argv[i] &lt;&lt; <span class="stringliteral">&quot;&#39;\n&quot;</span>;</div><div class="line">                <span class="keywordflow">return</span> EXIT_FAILURE;</div><div class="line">            }</div><div class="line"></div><div class="line">            <span class="keywordflow">if</span> (divide_images)</div><div class="line">            {</div><div class="line">                <a name="_a5"></a><a class="code" href="../../d2/d44/classcv_1_1Rect__.html">Rect</a> rect(0, 0, img.<a name="a6"></a><a class="code" href="../../d3/d63/classcv_1_1Mat.html#aa3e5a47585c9ef6a0842556739155e3e">cols</a> / 2, img.<a name="a7"></a><a class="code" href="../../d3/d63/classcv_1_1Mat.html#abed816466c45234254d25bc59c31245e">rows</a>);</div><div class="line">                imgs.push_back(img(rect).clone());</div><div class="line">                rect.x = img.<a class="code" href="../../d3/d63/classcv_1_1Mat.html#aa3e5a47585c9ef6a0842556739155e3e">cols</a> / 3;</div><div class="line">                imgs.push_back(img(rect).clone());</div><div class="line">                rect.x = img.<a class="code" href="../../d3/d63/classcv_1_1Mat.html#aa3e5a47585c9ef6a0842556739155e3e">cols</a> / 2;</div><div class="line">                imgs.push_back(img(rect).clone());</div><div class="line">            }</div><div class="line">            <span class="keywordflow">else</span></div><div class="line">                imgs.push_back(img);</div><div class="line">        }</div><div class="line">    }</div><div class="line">    <span class="keywordflow">return</span> EXIT_SUCCESS;</div><div class="line">}</div></div><!-- fragment --> </div><!-- contents -->
<!-- HTML footer for doxygen 1.8.6-->
<!-- start footer part -->
<hr class="footer"/><address class="footer"><small>
Generated on Fri Apr 2 2021 11:36:29 for OpenCV by &#160;<a href="http://www.doxygen.org/index.html">
<img class="footer" src="../../doxygen.png" alt="doxygen"/>
</a> 1.8.13
</small></address>
<script type="text/javascript">
//<![CDATA[
addTutorialsButtons();
//]]>
</script>
</body>
</html>
